<template>
  <div class="divBox relative">
    <el-card class="box-card">
      
      <div class="container mt-1">
        <!-- 公司信息区域 -->
        <el-form ref="infoForm" :model="companyInfo" label-width="120px" class="info-form">
		  <el-form-item label="登录名">
		    <el-input v-model="companyInfo.uname"  size="small"  disabled></el-input>
		  </el-form-item>
          <el-form-item label="公司名称">
            <el-input v-model="companyInfo.company_name" size="small" ></el-input>
          </el-form-item>
          
          <el-form-item label="公司地址">
            <el-input v-model="companyInfo.address" size="small" ></el-input>
          </el-form-item>
          
          <el-form-item label="联系人">
            <el-input v-model="companyInfo.name" size="small" ></el-input>
          </el-form-item>
          
          <el-form-item label="联系电话">
            <el-input v-model="companyInfo.phone" size="small" ></el-input>
          </el-form-item>
		  <!-- <el-form-item label="logo">
		     <uplodeImg ref="uplodeImg" @getImgUrl="getImgUrl" />
		  </el-form-item> -->
          <el-form-item label="">
            <el-button type="primary" size="small" @click="save">保存</el-button>
           <el-button  size="small" @click="openPasswordDialog">修改密码</el-button>
          </el-form-item>
        </el-form>
        
       
      </div>
    </el-card>
    
    <!-- 修改密码对话框 -->
    <el-dialog
      title="修改密码"
      :visible.sync="passwordDialogVisible"
      :close-on-click-modal="false"
      width="500px">
      
      <el-form ref="passwordForm" :model="passwordForm" label-width="120px">
        <el-form-item label="原密码" prop="oldPassword">
          <el-input v-model="passwordForm.oldPassword" type="password" size="small" placeholder="请输入原密码"></el-input>
        </el-form-item>
        
        <el-form-item label="新密码" prop="newPassword">
          <el-input v-model="passwordForm.newPassword" type="password" size="small" placeholder="请输入新密码"></el-input>
        </el-form-item>
        
        <el-form-item label="确认密码" prop="confirmPassword">
          <el-input v-model="passwordForm.confirmPassword" type="password" size="small" placeholder="请确认新密码"></el-input>
        </el-form-item>
      </el-form>
      
      <span slot="footer" class="dialog-footer">
        <el-button size="mini" @click="passwordDialogVisible = false">取 消</el-button>
        <el-button size="mini" type="primary" @click="submitPasswordForm">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
	import  uplodeImg from "@/components/uplodeImg.vue"
export default {
	components:{
		uplodeImg
	},
  data() {
    // 密码确认验证
    const validateConfirmPassword = (rule, value, callback) => {
      if (value !== this.passwordForm.newPassword) {
        callback(new Error('两次输入的密码不一致'));
      } else {
        callback();
      }
    };
    
    return {
      // 公司信息
      companyInfo: {
        name: '',
        address: '',
        company_name: '',
        phone: '',
		uname:"",
		logo:''
      },
      
      // 密码修改相关
      passwordDialogVisible: false,
      passwordForm: {
        oldPassword: '',
        newPassword: '',
        confirmPassword: ''
      },
      
      
    };
  },
  
  mounted() {
    // 页面加载时获取公司信息
    this.loadCompanyInfo();
  },
  
  methods: {
	  getImgUrl(val){
		  this.companyInfo.logo = val
	  },
	 save(){
		 this.$api.post('/admin/setCompanInfo',this.companyInfo).then(res => {
		   if (res.code === 200) {
			 this.$message.success('操作成功,请重新登陆！')
			 setTimeout(()=>{
				  this.$router.push('/')
			 })
		   }
		 });
	 },
    // 获取公司信息
    loadCompanyInfo() {
      // 实际项目中应替换为真实API请求
      this.$api.post('/admin/getCompanyInfo', {}).then(res => {
        if (res.code === 200) {
          this.companyInfo = res.data;
		  setTimeout(()=>{
		  	this.$refs.uplodeImg.url = res.data.logo
		  },100)
        }
      });
    },
    
    // 打开修改密码对话框
    openPasswordDialog() {
      this.passwordDialogVisible = true;
      this.$refs.passwordForm.resetFields();
    },
    
    // 提交密码修改
    submitPasswordForm() {
      this.$refs.passwordForm.validate((valid) => {
        if (valid) {
          // 实际项目中应替换为真实API请求
          this.$api.post('/admin/updateUpwd', this.passwordForm).then(res => {
            if (res.code === 200) {
              this.passwordDialogVisible = false;
              this.$message.success('操作成功,请重新登陆！')
              setTimeout(()=>{
                  this.$router.push('/')
              })
            } else {
              this.$message.error(res.msg || '密码修改失败');
            }
          });
        }
      });
    }
  }
}
</script>

<style lang="scss" scoped>
.divBox {
  
  .box-card {
    border-radius: 8px;
    box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1);
  }
  
  .font-w-b {
    font-weight: bold;
  }
  
  .font-16 {
    font-size: 16px;
  }
  
  .main-color-dan {
    color: #409EFF;
  }
  
  .mt-1 {
    margin-top: 10px;
  }
  
  .info-form {
    margin-bottom: 20px;
  }
  
  .password-btn {
    text-align: right;
    margin-top: 15px;
  }
}
</style>